import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseArrayPipe,
  ParseIntPipe,
  Post,
  Put,
  Query,
  Req,
  Res,
} from '@nestjs/common';
import { ParseIntArrayPipe } from '@/common/pipe/parse-int-array.pipe';
import Result from '@/common/result/Result';
import {
  ApiBody,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { RequirePermission } from '@/common/decorator/require-premission.decorator';
import { nowDateTime } from '@/common/utils';
import { RentalLeaseService } from './service/rental-lease.service';
import {
  QueryRentalLeaseDto,
  CreateRentalLeaseDto,
  UpdateRentalLeaseDto,
} from './dto/index';
import { Response } from 'express';
import { RentalLease } from '@prismaClient';
import { TableDataInfo } from '@/common/domain/TableDataInfo';
@ApiTags('租赁关系表')
@ApiBearerAuth()
@Controller('system/lease')
export class RentalLeaseController {
  constructor(private leaseService: RentalLeaseService) {}
  @ApiOperation({ summary: '查询房源表列表' })
  @ApiResponse({ type: Result<RentalLease[]> })
  @Get('/houseList')
  async getHouseList() {
    return Result.ok(await this.leaseService.getHouseList());
  }
  @ApiOperation({ summary: '查询租客表列表' })
  @ApiResponse({ type: Result<RentalLease[]> })
  @Get('/userList')
  async getUserList() {
    return Result.ok(await this.leaseService.getUserList());
  }
  @ApiOperation({ summary: '查询租赁关系表列表' })
  @ApiResponse({ type: TableDataInfo<RentalLease> })
  @RequirePermission('system:lease:list')
  @Get('/list')
  async listLease(
    @Query() q: QueryRentalLeaseDto,
  ): Promise<TableDataInfo<RentalLease>> {
    return Result.TableData(await this.leaseService.selectLeaseList(q));
  }
  @ApiOperation({ summary: '查询租赁关系表所有' })
  @ApiResponse({ type: Result<RentalLease[]> })
  @RequirePermission('system:lease:list')
  @Get('/data')
  async listLeaseData(): Promise<Result<RentalLease[]>> {
    return Result.ok(await this.leaseService.selectLeaseAll());
  }
  @ApiOperation({ summary: '导出租赁关系表xlsx文件' })
  @RequirePermission('system:lease:export')
  @Get('/export')
  async export(@Res() res: Response): Promise<void> {
    return this.leaseService.exportLease(res);
  }
  @ApiOperation({ summary: '查询租赁关系表详细' })
  @ApiResponse({ type: Result<RentalLease> })
  @RequirePermission('system:lease:list')
  @Get('/:id')
  async getLease(
    @Param('id', ParseIntPipe) id: number,
  ): Promise<Result<any>> {
    return Result.ok(await this.leaseService.selectLeaseById(id));
  }
  @ApiOperation({ summary: '新增租赁关系表' })
  @ApiResponse({ type: Result<RentalLease> })
  @ApiBody({ type: CreateRentalLeaseDto })
  @RequirePermission('system:lease:add')
  @Post('/')
  async addLease(
    @Body() rentalLease: CreateRentalLeaseDto,
    @Req() req,
  ): Promise<Result<RentalLease>> {
    rentalLease['createTime'] = nowDateTime();
    rentalLease['updateTime'] = nowDateTime();
    const { msg, data, code } = await this.leaseService.addLease(rentalLease);
    return code >= 400 ? Result.Error(msg) : Result.ok(data, msg);
  }
  @ApiOperation({ summary: '修改租赁关系表' })
  @ApiResponse({ type: Result<any> })
  @ApiBody({ type: UpdateRentalLeaseDto })
  @RequirePermission('system:lease:edit')
  @Put('/')
  async updateLease(
    @Body() rentalLease: UpdateRentalLeaseDto,
    @Req() req,
  ): Promise<Result<any>> {
    rentalLease['updateTime'] = nowDateTime();
    await this.leaseService.updateLease(rentalLease);
    return Result.ok('修改成功！');
  }
  @ApiOperation({ summary: '删除租赁关系表' })
  @ApiResponse({ type: Result<any> })
  @RequirePermission('system:lease:remove')
  @Delete('/:ids')
  async delLease(
    @Param('ids', ParseIntArrayPipe) ids: number[],
  ): Promise<Result<any>> {
    let { count, message, data } = await this.leaseService.deleteLeaseByIds(ids);
    return Result.toAjax(count);
  }
}
